package cn.comon.app.scan.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

public class FileDigest {
  
  private String getFileMD5(File file) {
    if (!file.isFile()){
      return null;
    }
    MessageDigest digest = null;
    FileInputStream in=null;
    byte buffer[] = new byte[1024];
    int len;
    try {
      digest = MessageDigest.getInstance("MD5");
      in = new FileInputStream(file);
      while ((len = in.read(buffer, 0, 1024)) != -1) {
        digest.update(buffer, 0, len);
      }
      in.close();
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
    BigInteger bigInt = new BigInteger(1, digest.digest());
    return bigInt.toString(16);
  }
  
  
  private Map<String, String> getDirMD5(File file,boolean listChild) {
    if(!file.isDirectory()){
      return null;
    }
    //<filepath,md5>
    Map<String, String> map=new HashMap<String, String>();
    String md5;
    File files[]=file.listFiles();
    for(int i=0;i<files.length;i++){
      File f=files[i];
      if(f.isDirectory()&&listChild){
        map.putAll(getDirMD5(f, listChild));
      } else {
        md5=getFileMD5(f);
        if(md5!=null){
          map.put(f.getPath(), md5);
        }
      }
    }
    return map;
  }
  
  /**
  * 下载指定URL路径的图片
  * @param path
   * @throws Exception
  */
  private void getImage(String path, String downloadURL) throws Exception {
      URL url = new URL(downloadURL);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      conn.setConnectTimeout(5000);
      if (conn.getResponseCode() == 200) {
          InputStream inputStream = conn.getInputStream();
          File file = new File("C:\\image.apk");
          FileOutputStream outStream = new FileOutputStream(file);
          int len = -1;
          byte[] b = new byte[1024];
          while((len = inputStream.read(b)) != -1){
              outStream.write(b, 0, len);
          }
          outStream.flush();
          outStream.close();
          inputStream.close();
      }
  }
  
  public boolean downLoadApp(String downloadURL) {
//  public static void main (String[] srgs) {
//    new InetAddressTest().performNSLookup();  
      FileDigest fileDigest = new FileDigest();
      File file = new File("D:\\我的文档\\下载\\mysql-5.5.28-win32.zip_1.exe");
      System.out.println(fileDigest.getFileMD5(file));
//      String path = "http://down.myapp.com/android_new/4/86004/886155/0391EC3AB42A640F6D3BF57689832E93.apk";
      try {
          String fileName = "";
          String temp[] = downloadURL.replaceAll("\\\\","/").split("/");   
          if (temp.length > 1) {   
              fileName = temp[temp.length - 1];   
          }
          String path = "";
          fileDigest.getImage(path, downloadURL);
          
          
          
//        fileDigest.getImage(path);
        System.out.println("end");
        return true;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        return true;
    }
     

}
}